<?php
$base_dir =  dirname(dirname(__FILE__));
$date = date('Y/m/d/',time());
//上传原图位置
$file_upload = $base_dir.'/docs/upload/'.$date;
//缩略图存储位置
$thumbnail = $file_upload.'thumbnail/';
//允许上传格式
$file_allow_ext='gif|jpg|jpeg|png|gif|zip|rar|ppt|xls|pdf|pptx|xlsx|docx';
//最大上传限制
$file_allow_size = 5*1024*1024;

if($_POST['submit']=="上传"){

    if(is_uploaded_file($_FILES['file']['tmp_name'])){
        //上传文件名
        $file_name = $_FILES['file']['name'];
        //上传错误码
        $file_error = $_FILES['file']['error'];
        //当前上传图片类型
        $file_type = $_FILES['file']['type'];
        //上传图片服务器临时存储位置
        $file_tmp_name = $_FILES['file']['tmp_name'];
        //当前上传图片大小
        $file_size = $_FILES['file']['size'];
        //获取文件后缀名
        $file_ext = substr($file_name, strrpos($file_name, '.')+1);

        switch($file_error){
            case 0:
                $data['status'] = 1000;
                $data['msg'] = "文件上传成功!";
                break;
            case 1:
                $data['status'] = 1001;
                $data['msg'] = "文件上传失败，文件大小".$file_size."超过限制,允许上传大小".sizeFormat($file_allow_size)."!";
                break;
            case 3:
                $data['status'] = 1002;
                $data['msg'] = "上传失败，文件只有部份上传!";
                break;
            case 4:
                $data['status'] = 1003;
                $data['msg'] = "上传失败，文件没有被上传!";
                break;
            case 5:
                $data['status'] = 1004;
                $data['msg'] = "文件上传失败，文件大小为0!";
                break;
        }

        if(stripos($file_allow_ext,$file_ext)===false){
            $data['status'] = 1005;
            $data['msg'] = "该文件扩展名不允许上传";
        }

        if($file_size>$file_allow_size){
            $data['status'] = 1006;
            $data['msg'] = "文件大小超过限制,只能上传".sizeFormat($file_allow_size)."的文件!";
        }

        if($data['status']==1000){  //上传失败
            //判断存储原图地址是否存在  不存在则创建目录
            if(!is_dir($file_upload)){
                mkdir($file_upload,'777',true);
            }
            //判断缩略图目录是否存在  不存在则创建目录
            if(!is_dir($thumbnail)){
                mkdir($thumbnail,'777',true);
            }
            //判断文件是否已存在
            if(file_exists($file_upload)){
                $file_new_name= generateRandomString().'.'.$file_ext;
                $file_save_path = $file_upload.$file_new_name;
                //移动临时文件至指定目录
                move_uploaded_file($file_tmp_name,$file_save_path);
                //缩略图
                if(file_exists($thumbnail)){
                    $thumbnail_new_name= generateRandomString().'.'.$file_ext;
                    $thumbnail_save_path = $thumbnail.$thumbnail_new_name;
                    $xy = ['x'=>600,'y'=>600];
                    thumb($file_save_path,$xy,$thumbnail_save_path);
                }
                $data['status'] = 0;
                $data['url'] = $file_save_path;
               var_dump($data);
            }else{
                var_dump($data);
            }
        }else{
            var_dump($data);
        }

    }

}

/**
 * 缩略图
 */
function thumb($file,$xy,$thumbnail_save_path){

    $x = $xy['x'];
    $y = $xy['y'];

    list($img_Width, $img_Height, $imgext, $attr)=getimagesize($file);

        if(($x/$img_Width)>($y/$img_Height)){
            $bl=$y/$img_Height;
        }else{
            $bl=$x/$img_Width;
        }
        $x=floor($img_Width*$bl); //缩放后
        $y=floor($img_Height*$bl);

        $images=imagecreatetruecolor($x,$y);

        switch($imgext){
            case 1:
                $imageout=imagecreatefromgif($file);
                break;
            case 2:
                $imageout=imagecreatefromjpeg($file);
                break;
            case 3:
                $imageout=imagecreatefrompng($file);
                break;
            default:
                $imageout=imagecreatefromjpeg($file);
        }
        imagecopyresized($images,$imageout,0,0,0,0,$x,$y,$img_Width,$img_Height);
        imagejpeg($images,$thumbnail_save_path);
}


/**
 * 获取随机文件名
 * 长度可控
 */
function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;

}


/**
 * 上传大小
 * 转换单位返回文件大小
 */
function sizeFormat($size){
    $b = 1024;
    $kb = $b*1024;
    $mb = $kb*1024;

    if($size<$b) {
        return $size."bytes";
    } else if($size<$kb) {
        $size=round($size/$b,1);
        return $size."KB";
    } else if($size<$mb) {
        $size=round($size/$kb,1);
        return $size."MB";
    } else {
        $size=round($size/$mb,1);
        return $size."GB";
    }
}







